package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainActivity extends AppCompatActivity {
    private String matchId = "1234567";
    private MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myView = findViewById(R.id.view_gobang);

        //启动一个定时器，每隔一定的时间查询该盘的对阵
        setTimerForQuery();
    }

    private void setTimerForQuery() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                BmobQuery<WuziData> wuziDataBmobQuery = new BmobQuery<>();
                wuziDataBmobQuery.addWhereEqualTo("matchId", matchId);
                wuziDataBmobQuery.findObjects(new FindListener<WuziData>() {
                    @Override
                    public void done(List<WuziData> list, BmobException e) {
                        if(e == null && list != null && list.size() > 0){
                            WuziData wuziData = list.get(0);
                            //将五子棋的界面刷新
                            myView.setData(wuziData);
                        }
                    }
                });
            }
        }, 1000, 1000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.action_settings){
            myView.start();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
